﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class ForumDetails : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ForumDetails_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                this.MarkAllAsReadButton.Visible = false;
                this.CreateNewThreadButton.Visible = false;
                this.LeaveThisForumButton.Visible = false;
            }
            
            OnlineUser ou = (OnlineUser)Session["User"];
            bool IsPublic;
            string ForumId = null;
            string NumApplications;
            if (Request.QueryString["Id"] != null)
            {
                ForumId = Request.QueryString["Id"].ToString();
                ForumIdHiddenField.Value = ForumId;
            }
            else
                Response.Redirect("~/ForumHomePage.aspx");
            //string Answer = null;
            if (Request.QueryString["Answer"] != null)
                this.MessageLabel.Text = Request.QueryString["Answer"].ToString();
            
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            //this.Title = GND.ReturnAppTitle();

            //if (ou.TeamId == "Null")
            //    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

            ForumNameLabel.Text = GND.ReadNivdalStringData("@ForumId", "Nivdal_ForumGetForumName", ForumId);
            ForumNameLabel2.Text = ForumNameLabel.Text;

            IsPublic = GND.ReadNivdalBoolData("@ForumId", "Nivdal_ForumIsForumPublic", ForumId);
            if ((!IsPublic) && (!GND.ReadNivdalBoolData("@UserId", "@ForumId", "Nivdal_ForumIsUserForumMember", ou.UserId, ForumId)) )
                Response.Redirect("~/JoinPrivateForum.aspx?ForumId=" + ForumId + "&Answer=" + Resources.Resource.ForumDetails_NotMemeber);
            this.ForumDetailsSqlDataSource.SelectParameters.Add("ForumId", ForumId);
            this.ForumDetailsSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
            if (!IsPostBack)
            {
                this.GetForumThreadsSqlDataSource.SelectParameters.Add("ForumId", ForumId);
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                 this.GetForumThreadsSqlDataSource.SelectParameters.Add("UserId", ou.UserId);
                LeaveThisForumButton.OnClientClick = LeaveThisForumOnClientClick();
            }



            if ((GND.ReadNivdalBoolData("@UserId", "@ForumId", "Nivdal_ForumCanUserAdministerForum", ou.UserId, ForumId)) && (!IsPublic))
            {
                //this.MessageLabel.Text = GND.ReadNivdalBoolData("@UserId", "@ForumId", "CanUserAdministerForum", UsrId, ForumId).ToString();
                this.ToTorumAdminPageLinkButton.NavigateUrl = "~/AdministerForum.aspx?ForumId=" + ForumId;
                this.ToTorumAdminPageLinkButton.Visible = true;
                NumApplications = GND.ReadNivdalIntData("@ForumId", "Nivdal_ForumGetNumForumAppications", ForumId);
                if (NumApplications != "0")
                    this.ToTorumAdminPageLinkButton.Text += ("(" + NumApplications + ")");

            }
            else if (!IsPublic)
            {
                this.BecomeForumAdminLinkButton.PostBackUrl = "~/ApplyForforumAdmin.aspx?ForumId=" + ForumId;
                this.BecomeForumAdminLinkButton.Visible = true;
            }


            if (!IsPublic)
            {
                //this.MessageLabel.Text = GND.ReadNivdalBoolData("@ForumId", "Nivdal_ForumIsForumPublic", ForumId).ToString();
                this.ForumMembersLinkButton.NavigateUrl = "~/ForumMembers.aspx?ForumId=" + ForumId;
                this.ForumMembersLinkButton.Visible = true;

            }


        }

        protected void CreateNewThreadButton_Click(object sender, EventArgs e)
        {
            string ForumId = ForumIdHiddenField.Value;
            Response.Redirect("~/CreateForumThread.aspx?Id=" + ForumId);
        }


        protected void LeaveThisForumButton_Click(object sender, EventArgs e)
        {
            string ForumId = ForumIdHiddenField.Value;

            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ForumUserLeaveForum";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_ForumId = new SqlParameter();
            dbParam_ForumId.ParameterName = "@ForumId";
            dbParam_ForumId.Value = ForumId;
            dbParam_ForumId.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_ForumId);

            SqlParameter dbParam_UserId = new SqlParameter();
            dbParam_UserId.ParameterName = "@UserId";
            dbParam_UserId.Value = ou.UserId;
            dbParam_UserId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_UserId);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                Response.Redirect("ForumHomePage.aspx?Answer=" + dbParam_Answer.Value.ToString());
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
                return;

            }

            finally
            {
                dbConnection.Close();
            }



        }


        protected string IconName(object IsClosed, object IsSticky)
        {
            string ReturnValue = "~/Images/Icons/";
            bool Closed = (bool)IsClosed;
            bool Sticky = (bool)IsSticky;
            if (Sticky)
                ReturnValue += "pin.png";
            else if (Closed)
                ReturnValue += "ThreadClosed.png";
            else
                ReturnValue += "ThreadOpen.png";

            return (ReturnValue);
        }

        protected string ThreadIconToolTip(object IsClosed, object IsSticky)
        {
            string ReturnValue;
            bool Closed = (bool)IsClosed;
            bool Sticky = (bool)IsSticky;
            if (Sticky)
                ReturnValue = Resources.Resource.Forum_PinnedThread;
            else if (Closed)
                ReturnValue = Resources.Resource.Forum_ClosedThread;
            else
                ReturnValue = Resources.Resource.Forum_OpenThread;

            return (ReturnValue);
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string LeaveThisForumOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.ForumDetails_LeaveThisForumOnClientClick + "')");
        }

        protected void MarkAllAsRead(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = GND.ReadNivdalStringData("UserId", "ForumId", "Nivdal_ForumMarkAllSpecificForumThreadsAsRead", ou.UserId, ForumIdHiddenField.Value);
            Response.Redirect("ForumDetails.aspx?Id=" + ForumIdHiddenField.Value + "&Answer=" + Answer);
        }

    }
}